/**
 * addFront：队列前端添加元素
 * addBack：队列后端添加元素
 * removeFront：队列前端移除元素
 * peekBack：队列后端移除元素
 * isEmpty：判断是否为空
 * size：返回栈里元素的长度
 * clear：清空栈里的元素
 * toString：数组中的toString方法
 *
 * @class Deque
 */
class Deque{
  constructor(){
    this._count = 0;
    this._lowestCount = 0;
    this._items = {};
  }

  addFront(elem){
    if(this.isEmpty()){
      this.addBack(elem);
    }else if(this._lowestCount > 0){
      this._lowestCount --;
      this._items[this._lowestCount] = elem;
    }else {
      for(let i = this._count; i > 0; i--){
        this._items[i] = this._items[i-1];
      }
      this._count++;
      this._lowestCount = 0;
      this._items[0] = elem;
    }
  }

  addBack(...elem){
    for(let i = 0; i < elem.length; i++){
      this._items[this._count] = elem[i];
      this._count ++;
    }
  }

  removeFront(){
    if(this.isEmpty()){
      return undefined;
    }

    const result = this._items[this._lowestCount];
    delete this._items[this._lowestCount];
    this._lowestCount ++;
    return result;
  }

  removeBack(){
    if(this.isEmpty()){
      return undefined;
    }

    this._count --;
    const result = this._items[this._count];
    delete this._items[this._count];
    return result;
  }

  peekFront(){
    if(this.isEmpty()){
      return undefined;
    }

    return this._items[this._lowestCount];
  }

  peekBack(){
    if(this.isEmpty()){
      return undefined;
    }

    return this._items[this._count - 1];
  }

  isEmpty(){
    return this._count - this._lowestCount === 0;
  }

  size(){
    return this._count - this._lowestCount;
  }

  clear(){
    this._items = {};
    this._count = 0;
    this._lowestCount = 0;
  }

  toString(){
    if(this.isEmpty()){
      return '';
    }

    let objString = `${this._items[this._lowestCount]}`;
    for(let i = this._lowestCount + 1; i < this._count; i++ )
      objString = `${objString},${this._items[i]}`;
    
    return objString;
  }
}

const deque = new Deque();
console.log(deque.isEmpty()); // true

deque.addBack('john');
deque.addBack('jack');
console.log(deque.toString());  // john,jack

deque.addBack('camila');
console.log(deque.toString()); // john,jack,camila
console.log(deque.size());  // 3
console.log(deque.isEmpty()); // false

deque.removeFront();
console.log(deque.toString()); // jack,camila

deque.addFront('john');
console.log(deque.toString()); // john,jack,camila


// 回文检查器
const palindromeChecker = (aString) => {
  if(aString === undefined || aString === null || (aString !== null && aString.length === 0)){
    return false;
  }

  const deque = new Deque();
  const lowerString = aString.toLocaleLowerCase().split(' ').join('');
  let isEqual = true;
  let fistChar, lastChat;

  for(let i = 0; i < lowerString.length; i++){
    deque.addBack(lowerString.charAt(i));
  }

  while(deque.size() > 1 && isEqual){
    fistChar = deque.removeFront();
    lastChat =  deque.removeBack();
    if(fistChar !== lastChat){
      isEqual = false;
    }
  }

  return isEqual;
}

console.log('a', palindromeChecker('a'))
console.log('abc', palindromeChecker('abc'))
console.log('aba', palindromeChecker('aba'))

// ''.split('').reverse().join('') === ''